import 'package:canteen/constants.dart';
import 'package:canteen/page/login_page.dart';
import 'package:canteen/page/register_page.dart';
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';

import '../class/user.dart';
import '../dio/configure_dio.dart';
import '../dio/user_dio.dart';
import '../userDetail.dart';
import 'home_page.dart';

class InitPage extends StatefulWidget {
  static String id = "init_page";

  const InitPage({super.key});

  @override
  State<InitPage> createState() => _InitPageState();
}

class _InitPageState extends State<InitPage> {
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    if (selfLogin == 0) {
      loginBySharedPreferences();
    }
  }

  Future<void> loginBySharedPreferences() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    String password = prefs.getString('password') ?? '';
    String username = prefs.getString('username') ?? '';
    if (username != '' && password != '') {
      try {
        Response response = await login(
            user: {'username': username, 'password': password}, dio: dio);
        if (response.data['data'].length != 0) {
          user = User(
            id: response.data['data']['id'],
            username: username,
            password: password,
            type: response.data['data']['type'],
            department: response.data['data']['department'] ?? '无',
          );
          selfLogin++;
          Navigator.pushNamedAndRemoveUntil(
              context, HomePage.id, (route) => false);
        }
      } on Exception catch (e) {}
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      body: Padding(
        padding: const EdgeInsets.symmetric(horizontal: 65.0),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            Container(
              child: Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Expanded(
                    child: Hero(
                      tag: 'logo',
                      child: Container(
                        child: Image.asset(
                          'images/peanutB.png',
                        ),
                      ),
                    ),
                  ),
                  SizedBox(
                    width: 10,
                  ),
                  Expanded(
                    flex: 3,
                    child: Container(
                      child: Image.asset(
                        'images/peanutB1.jpg',
                      ),
                    ),
                  )
                ],
              ),
            ),
            SizedBox(
              height: 70,
            ),
            Container(
              width: 150,
              padding: EdgeInsets.symmetric(vertical: 10),
              decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(25), // 圆角半径
                  color: Colors.white,
                  boxShadow: [
                    BoxShadow(
                      offset: Offset(0, 1),
                      blurRadius: 2,
                      color: Colors.black.withOpacity(0.2),
                    )
                  ]),
              child: Column(
                children: [
                  TextButton(
                    child: Text(
                      '登录',
                      style: TextStyle(color: Colors.black, fontSize: 18),
                    ),
                    onPressed: () {
                      Navigator.pushNamed(context, LoginPage.id);
                    },
                  ),
                  Container(
                    height: 1,
                    width: 25,
                    color: Colors.black,
                  ),
                  TextButton(
                    child: Text(
                      '注册',
                      style: TextStyle(color: Colors.black, fontSize: 18),
                    ),
                    onPressed: () {
                      Navigator.pushNamed(context, RegisterPage.id);
                    },
                  ),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }
}
